package be.phl.obstacleapp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import be.phl.model.DBAdapterPatient;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ContactpersonFragment extends Fragment {

	Button btnSave;
	EditText txtContactFirstName, txtContactLastName, txtContactRelation,
			txtContactEmail, txtContactNumber;
	private int PatientId;
	private TextView mConnectionError, txtV, txtA, txtR, txtE, txtT;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstances) {
		return inflater.inflate(R.layout.fragment_contactperson, container,
				false);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		txtContactFirstName = (EditText) getActivity().findViewById(
				R.id.txtContactFirstName);
		txtContactLastName = (EditText) getActivity().findViewById(
				R.id.txtContactLastName);
		txtContactRelation = (EditText) getActivity().findViewById(
				R.id.txtContactRelation);
		txtContactEmail = (EditText) getActivity().findViewById(
				R.id.txtContactEmail);
		txtContactNumber = (EditText) getActivity().findViewById(
				R.id.txtContactNumber);
		btnSave = (Button) getActivity().findViewById(R.id.btnSave);
		txtV = (TextView) getActivity().findViewById(R.id.txtV);
		txtA = (TextView) getActivity().findViewById(R.id.txtA);
		txtR = (TextView) getActivity().findViewById(R.id.txtR);
		txtE = (TextView) getActivity().findViewById(R.id.txtE);
		txtT = (TextView) getActivity().findViewById(R.id.txtT);

		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),
				"com.ttf");

		txtV.setTypeface(tf, Typeface.BOLD);
		txtA.setTypeface(tf, Typeface.BOLD);
		txtR.setTypeface(tf, Typeface.BOLD);
		txtE.setTypeface(tf, Typeface.BOLD);
		txtT.setTypeface(tf, Typeface.BOLD);
		btnSave.setTypeface(tf, Typeface.BOLD);

		btnSave.setOnClickListener(btnOnClickListener);
		mConnectionError = (TextView) getActivity().findViewById(
				R.id.connection_error);

		PatientId = (int) getActivity().getIntent().getIntExtra("PatientId", 0);
		DBAdapterPatient db = new DBAdapterPatient(getActivity());

		db.open();
		Cursor p = db.getContact(PatientId);

		while (p.moveToNext()) {
			txtContactFirstName.setText(p.getString(1));
			txtContactLastName.setText(p.getString(2));
			txtContactRelation.setText(p.getString(3));
			txtContactEmail.setText(p.getString(4));
			txtContactNumber.setText(p.getString(5));

		}
		db.close();
	}

	Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			Pattern p = Pattern.compile("([0-9]*)");

			Matcher m = p.matcher(txtContactNumber.getText().toString());
			if (txtContactFirstName.getText().toString().equals("")) {
				txtContactFirstName.setError("Mag niet leeg zijn");
				txtContactFirstName.requestFocus();
			} else if (txtContactLastName.getText().toString().equals("")) {
				txtContactLastName.setError("Mag niet leeg zijn");
				txtContactLastName.requestFocus();
			} else if (txtContactRelation.getText().toString().equals("")) {
				txtContactRelation.setError("Mag niet leeg zijn");
				txtContactRelation.requestFocus();
			} else if (txtContactEmail.getText().toString().equals("")) {
				txtContactEmail.setError("Mag niet leeg zijn");
				txtContactEmail.requestFocus();
			} else if (!android.util.Patterns.EMAIL_ADDRESS.matcher(
					txtContactEmail.getText().toString()).matches()) {
				txtContactEmail.setError("Geen correct email-adres");
				txtContactEmail.requestFocus();
			}

			else if (txtContactNumber.getText().toString().equals("")) {
				txtContactNumber.setError("Mag niet leeg zijn");
				txtContactNumber.requestFocus();
			} else if (!m.matches()) {
				txtContactNumber.setError("Mag enkel uit nummers bestaan");
				txtContactNumber.requestFocus();
			} else {

				PatientId = (int) getActivity().getIntent().getIntExtra(
						"PatientId", 0);
				DBAdapterPatient db = new DBAdapterPatient(getActivity());
				db.open();

				if (db.updateContact(PatientId, txtContactFirstName.getText()
						.toString(), txtContactLastName.getText().toString(),
						txtContactRelation.getText().toString(),
						txtContactEmail.getText().toString(), txtContactNumber
								.getText().toString()) > 0) {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Aanpassen succesvol!";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				} else {
					Context context = getActivity().getApplicationContext();
					CharSequence text = "Aanpassen mislukt!";
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}

				db.close();
			}
		}
	};

}
